Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

284
Visualizações
Is there any reason to use ZoneId.of("UTC") instead of ZoneOffset.UTC?

Is there any reason to use ZoneId.of("UTC") instead of ZoneOffset.UTC?

We know the difference between the two as provided in What is the difference between ZoneOffset.UTC and ZoneId.of("UTC")?.

<tl;dr> version:

  • ZoneOffset.UTC returns a mere ZoneOffset with ID "Z", offset of 0 and default zone rules.
  • ZoneId.of("UTC") returns a ZoneRegion with ID "UTC" and ZoneOffset.UTC included.

</tl;dr>

For this question, I assume UTC-usage merely for easier date and time handling and not, because something might actually be located in the UTC region or some other business reason to have this as the actual ZoneRegion.

For example, when dealing with ZonedDateTime. The only difference I could find was that it prints differently.

2021-06-10T15:28:25.000000111Z
2021-06-10T15:28:25.000000111Z[UTC]

We are having code review discussions back and forth about this, so I guess this conflict is not uncommon.

Here are my thoughts so far

ZoneOffset.UTC

  • It is a constant (and further, it's Offset value (0) is even cached).
  • It has (a tiny bit) less overhead, due to the missing region information.
  • At UTC, there are no daylight saving times or historical shifts to consider, like in any other timezone.
    Thus, an Offset of 0 is in general enough for all use cases I encountered so far (like converting to and from a certain Zone Region with a particular daylight saving status).

ZoneId.of("UTC")

  • In general, I'd say ZoneRegions are preferred to ZoneOffsets, due to a range of additional location-specific data like a particular daylight saving time or time shifts in history.
  • However, in case of UTC, ZoneId.of("UTC") is just a region wrap around ZoneOffset.UTC and I could not find any benefit so far. As far as I know, in UTC no region-relevant data exists apart from it's inherited ZoneOffset.UTC rules.
  • Needs to be parsed every time.

So my _guess_ is:

Unless you really need the UTC time zone/region for some (e.g. business) reasons, you should prefer ZoneOffset.UTC. It has a (minor) advantage in its performance footprint and the UTC ZoneRegion does not seem provide any benefit I can see or think of.

However, because of the complexity of the Java Date Time API it is hard to tell if I am missing something in this discussion.
Is there any reason why someone should ever use ZoneId.of("UTC")?

over 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

Is there any reason why someone should ever use ZoneId.of("UTC")?

I cannot (right now) think of any good reasons.

However, it is not egregiously bad to do that. There would most likely be a small runtime cost in doing the lookup, but it is unlikely to matter. And the two forms are (IMO) equally readable.


However, if the zone name is parameter you could find code like this:

   String zoneName = "UTC";  // my default
   // Try to get a non-default zone
   // ...
   id = ZoneId.of(zoneName);

which would (IMO) be better than this:

   String zoneName = null;
   // Try to get a non-default zone
   // ...
   id = zoneName == null ? ZoneId.UTC : ZoneId.of(zoneName);
over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda